// 码云相关的 Mock 数据

// 码云 OAuth 配置
export const GITEE_OAUTH_CONFIG = {
  AUTH_URL: 'https://gitee.com/oauth/authorize',
  CLIENT_ID: '129cb4f434f494a79fb6bf8cb5d3a0f26df1b941cf435f891b8f67769f657d1c',
  REDIRECT_URI: 'http://localhost:5173/oauth',
  SCOPE: 'user_info',
  STATE_PREFIX: 'gitee_login_'
} as const;

// 码云用户信息接口
export interface GiteeUserInfo {
  id: number;
  login: string;
  name: string;
  avatar_url: string;
  email: string;
  bio: string;
  created_at: string;
  updated_at: string;
}

// 模拟码云用户信息
export const mockGiteeUserInfo: GiteeUserInfo = {
  id: 12345,
  login: 'testuser',
  name: '测试用户',
  avatar_url: 'https://gitee.com/assets/favicon.ico',
  email: 'test@gitee.com',
  bio: '这是一个测试账号',
  created_at: '2024-01-01T00:00:00Z',
  updated_at: new Date().toISOString()
};

// 模拟获取码云 access_token
export const mockGiteeAccessToken = async () => {
  // 模拟网络请求延迟
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  // 返回模拟的 token（实际应该从后端获取）
  return {
    access_token: 'mock_gitee_token_' + Date.now(),
    token_type: 'bearer',
    expires_in: 7200
  };
};

// 模拟获取码云用户信息
export const mockGiteeUserInfoAPI = async (): Promise<GiteeUserInfo> => {
  // 实际应该调用码云 API
  // const response = await fetch('https://gitee.com/api/v5/user', {
  //   headers: {
  //     'Authorization': `Bearer ${accessToken}`
  //   }
  // });
  // return await response.json();

  // 模拟用户信息
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  return {
    ...mockGiteeUserInfo,
    updated_at: new Date().toISOString()
  };
};

// 模拟码云登录成功
export const mockGiteeLoginSuccess = async (userInfo: GiteeUserInfo) => {
  // 模拟网络请求延迟
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  // 构建用户数据
  const userData = {
    token: 'gitee_token_' + Date.now(),
    refreshToken: 'gitee_refresh_token_' + Date.now(),
    user: {
      id: userInfo.id,
      phone: '', // 码云登录没有手机号
      name: userInfo.name || userInfo.login,
      avatar: userInfo.avatar_url,
      role: '患者',
      department: '内科',
      email: userInfo.email,
      loginType: 'gitee',
      giteeId: userInfo.id,
      lastLoginTime: new Date().toISOString()
    }
  };
  
  return userData;
};

// 生成码云 OAuth 授权 URL
export const generateGiteeAuthUrl = () => {
  const state = GITEE_OAUTH_CONFIG.STATE_PREFIX + Date.now();
  
  const params = new URLSearchParams({
    client_id: GITEE_OAUTH_CONFIG.CLIENT_ID,
    redirect_uri: GITEE_OAUTH_CONFIG.REDIRECT_URI,
    response_type: 'code',
    scope: GITEE_OAUTH_CONFIG.SCOPE,
    state: state
  });
  
  return {
    url: `${GITEE_OAUTH_CONFIG.AUTH_URL}?${params.toString()}`,
    state: state
  };
};
